﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace Cinser.Common.Silverlight.DT
{
    /*
     * DataColumn对象的控制器
     * 实现DataColumn对象的添加，删除功能，
     * */
    public class DMPColumnCollection
    {
        //构造
        public DMPColumnCollection()
        {
            this.listColumn = new List<DMPDataColumn>();
        }
        /// <summary>
        /// 拷贝构造
        /// </summary>
        /// <param name="column"></param>
        public DMPColumnCollection(DMPColumnCollection column)
        {
            this.listColumn = new List<DMPDataColumn>();
            foreach (DMPDataColumn c in column.listColumn)
            {
                this.listColumn.Add(new DMPDataColumn(c.ColumnName,c.DataType));
            }
        }


        #region 方法

        /// <summary>
        /// 添加DataColumn到表中
        /// </summary>
        /// <param name="name">DataColumn</param>
        /// <param name="type">该列的类型</param>
        /// <returns>返回添加的DataColumn</returns>
        public DMPDataColumn Add(string name, Type type)
        {
            if (name != null && type != null)
            {
                DMPDataColumn column = new DMPDataColumn(name, type);
                //关联到表
                column.SetTable(this.table);
                //如果该列已经存在，抛出异常
                if (this.ColumnExists(column))
                {
                    throw new InvalidOperationException("this column has existed");
                }
                //否则添加到表中
                else
                {
                    listColumn.Add(column);
                    if (table.Rows.Count != 0)
                    {
                        //对每一行做出更改
                        int index = this.InedxOf(column);
                        table.Rows.InsertToByColumn(column.DefaultValue, index);
                    }
                }
                return column;
            }
            return null;
        }

        /// <summary>
        /// 复制添加DataColumn到表中
        /// </summary>
        /// <param name="column">要添加的DataColumn</param>
        public void Add(DMPDataColumn column)
        {
            if (column != null)
            {
                //如果该列已经存在，抛出异常
                if (this.ColumnExists(column))
                {
                    throw new InvalidOperationException("this column has existed");
                }
                //否则，添加到表中
                else
                {
                    //关联到表
                    column.SetTable(this.table);
                    listColumn.Add(column);
                    if (table.Rows.Count != 0)
                    {
                        //对每一行做出更改
                        int index = this.InedxOf(column);
                        table.Rows.InsertToByColumn(column.DefaultValue, index);

                    }
                }

            }

        }


        /// <summary>
        /// 删除表名为name的项
        /// </summary>
        /// <param name="name">string</param>
        public void Remove(string name)
        {
            int index = InedxOf(name);
            listColumn.RemoveAt(index);
            //对每一行做出更改
            table.Rows.RemoveByColumn(index);
        }

        /// <summary>
        /// 删除指定的列
        /// </summary>
        /// <param name="column">DataColumn</param>
        public void Remove(DMPDataColumn column)
        {
            int index = InedxOf(column);
            listColumn.RemoveAt(index);
            //对每一行做出更改
            table.Rows.RemoveByColumn(index);
        }


        /// <summary>
        /// 删除指定位置的列
        /// </summary>
        /// <param name="index">位置索引</param>
        public void RemoveAt(int index)
        {
            listColumn.RemoveAt(index);
            //对每一行做出更改
            table.Rows.RemoveByColumn(index);
        }

        /// <summary>
        /// 清空
        /// </summary>
        public void Clear()
        {
            listColumn.Clear();
        }

        /// <summary>
        /// 返回指定DataColumn的索引号
        /// </summary>
        /// <param name="column">DataColumn</param>
        /// <returns>找到返回索引号，否则返回-1</returns>
        public int InedxOf(DMPDataColumn column)
        {
            if (column != null)
            {
                return listColumn.IndexOf(column);
            }
            else throw new InvalidOperationException();
        }


        /// <summary>
        /// 返回指定DataColumn的索引号
        /// </summary>
        /// <param name="name">string</param>
        /// <returns>找到返回索引号，否则返回-1</returns>
        public int InedxOf(string name)
        {
            if (name != null)
            {
                int n = 0;
                foreach (DMPDataColumn c in listColumn)
                {
                    if (c.ColumnName == name)
                    {
                        return n;
                    }
                    n++;
                }
                return -1;
            }
            else throw new InvalidOperationException();
        }


        /// <summary>
        /// 设置该CloumnCollection所属的表
        /// </summary>
        /// <param name="table">表</param>
        internal void SetTable(DMPDataTable table)
        {
            this.table = table;
        }
        #region 索引

        public DMPDataColumn this[int index]
        {
            get
            {
                return listColumn[index];
            }
        }


        public DMPDataColumn this[string name]
        {
            get
            {
                return listColumn[InedxOf(name)];
            }
        }



        #endregion

        /// <summary>
        /// 判断列是否已经存在
        /// </summary>
        /// <param name="column"></param>
        /// <returns>已经存在返回TRUE，否则返回FALSE</returns>
        private bool ColumnExists(DMPDataColumn column)
        {
            foreach (DMPDataColumn c in listColumn)
            {
                if (c.Equals(column))
                {
                    return true;
                }
            }
            return false;
        }


        #endregion


        #region  属性
        //列的数量
        public int Count
        {
            get
            {
                return listColumn.Count;

            }
        }


        private DMPDataTable table;
        public DMPDataTable Table
        {
            get { return table; }
        }

        #endregion



        //实现对foreach的支持
        //////////////////////////////////////////////////////////////////////////
        public IEnumerator GetEnumerator()
        {
            return new CloumnCollectionEnumerator(this);
        }
        private class CloumnCollectionEnumerator : IEnumerator
        {
            public CloumnCollectionEnumerator(DMPColumnCollection c)
            {
                this.cloumnCollection = c;
            }
            private int positon = -1;
            private DMPColumnCollection cloumnCollection;
            public bool MoveNext()
            {

                if (positon < cloumnCollection.listColumn.Count - 1)
                {
                    positon++;
                    return true;
                }
                return false;
            }
            public void Reset()
            {
                positon = -1;
            }
            public object Current
            {
                get
                {
                    return cloumnCollection.listColumn[positon];
                }
            }

        }

        //////////////////////////////////////////////////////////////////////////






        public List<DMPDataColumn> listColumn = new List<DMPDataColumn>();
    }
}
